<template>
  <div class="move-menu">
    <div class="move-menu-main">
      <div
        class="move-menu-item"
        v-for="(item, index) in isparent ? nodesParent : nodes"
        :key="index"
        @mouseover="nextHover(item.id, index, item)"
        @mouseleave="nextLeave(index)"
        @click.stop="nodeClick(item.id)"
      >
        <div class="move-menu-item-left">
          <div>{{ item.name }}</div>
        </div>
        <img
          src="https://image.xuexiwangzhe.com/FlPspKXYPYyP4rPiVoTfgtizauvI"
          class="move-menu-item-right-img"
          v-if="item.children && item.children.length > 0"
        />
        <move-tree-menu
          v-if="item.children && item.children.length > 0"
          v-show="childrenShow[index]"
          :nodes="item.children"
          :folderid="folderid"
          :isparent="false"
          v-bind="$attrs"
          v-on="$listeners"
        ></move-tree-menu>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "moveTreeMenu",
  props: {
    isparent: {
      type: Boolean,
      default: true,
    },
    nodes: {
      type: Array,
      default: () => {
        return [];
      },
    },
    folderid: {
      type: Number,
    },
    folderdata: {
      type: Object,
    },
  },
  data() {
    return {
      childrenShow: [],
      nodesParent: [],
    };
  },
  created() {
    if (this.isparent) {
      this.getData();
    }
  },
  methods: {
    nextHover(id, index, data) {
      if (
        !this.childrenShow[index] &&
        data.children &&
        data.children.length > 0
      ) {
        this.$set(this.childrenShow, index, true);
      }
    },
    nodeClick(id) {
      this.$axios
        .get("/folder/move", {
          params: { folderId: this.folderid, moveFolderId: id },
        })
        .then((res) => {
          if (res.success) {
            this.$emit("move-folder");
            this.$store.commit("setListOperate", "move-folder");
            this.$store.commit("setListOperateData", {
              folderId: this.folderid,
              moveFolderId: id,
              folderData: this.folderdata,
              parentId: this.folderdata.parentId || -10,
            });
          }
        });
    },
    nextLeave(index) {
      this.$set(this.childrenShow, index, false);
    },
    getData() {
      this.$axios.get("/folder/findFolderTree").then((res) => {
        if (res.success) {
          this.nodesParent = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.move-menu {
  position: absolute;
  z-index: 10;
  top: 0;
  left: calc(100%);
  padding-left: 3px;
}

.move-menu-main {
  background: #ffffff;
  box-shadow: 0px 2px 6px 0px rgba(102, 102, 102, 0.2);
  border-radius: 3px;
}

.move-menu-item {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 15px;
  padding-right: 5px;
  padding-top: 6px;
  padding-bottom: 6px;
  width: 116px;
  box-sizing: border-box;
  margin-bottom: 6px;
  color: #666;
  cursor: pointer;

  &:last-child {
    margin-bottom: 0;
  }

  &:hover {
    background-color: #999999;
    color: #fff;
  }
}

.move-menu-item-left {
  display: flex;
  align-items: center;
}

.move-menu-item-left-img {
  width: 12px;
  margin-right: 9px;
}

.move-menu-item-right-img {
  width: 12px;
}
</style>
